﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

using Microsoft.Win32;

using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Common.Files;
using SBPweb.MediaCenter.Common.Services;

namespace SBPweb.MediaCenter.Modules.Viewer.WPF.Controls
{
    /// <summary>
    /// Interaction logic for Toolbar.xaml
    /// </summary>
    public partial class Toolbar : UserControl
    {

        #region Private fields

        private IModule parentModule;
        private int lastFilterIndex = 1;

        #endregion

        #region Constructor logic
        
        public Toolbar()
        {
            InitializeComponent();
        }

        public Toolbar(IModule parentModule)
            : this()
        {
            this.parentModule = parentModule;
        }

        #endregion

        #region Property accessors

        public IModule ParentModule
        {
            get
            {
                return parentModule;
            }
            set
            {
                parentModule = value;
            }
        }

        public IUriListOpenerService ListOpener
        {
            get
            {
                return parentModule.Services.Get<IUriListOpenerService>();
            }
        }

        public Controllers.ViewerService Viewer
        {
            get
            {
                return parentModule.Services.Get<IViewerService>() as Controllers.ViewerService;
            }
        }

        #endregion

        #region Methods

        public void SetEnabled(bool? isEnabled)
        {
            if (isEnabled.HasValue)
            {
                bInfo.IsEnabled = isEnabled.Value;
                bSave.IsEnabled = isEnabled.Value;
                bDelete.IsEnabled = isEnabled.Value;
                bCopy.IsEnabled = isEnabled.Value;
                bCut.IsEnabled = isEnabled.Value;
                bPaste.IsEnabled = isEnabled.Value;
                bUndo.IsEnabled = isEnabled.Value;
                bZoomIn.IsEnabled = isEnabled.Value;
                bZoomOut.IsEnabled = isEnabled.Value;
                bPrev.IsEnabled = isEnabled.Value;
                bNext.IsEnabled = isEnabled.Value;
                bUp.IsEnabled = isEnabled.Value;
                bDown.IsEnabled = isEnabled.Value;
            }
            else
            {
                if (ListOpener != null)
                {
                    bPrev.IsEnabled = ListOpener.IsPreviousAvailable;
                    bNext.IsEnabled = ListOpener.IsNextAvailable;
                }
            }
        }

        #region UI event handlers

        private void Contents_MouseEnter(object sender, MouseEventArgs e)
        {
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.WidthProperty, 64, TimeSpan.FromMilliseconds(50));
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.HeightProperty, 64, TimeSpan.FromMilliseconds(50));
        }

        private void Contents_MouseLeave(object sender, MouseEventArgs e)
        {
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.WidthProperty, 48, TimeSpan.FromMilliseconds(300));
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.HeightProperty, 48, TimeSpan.FromMilliseconds(300));
        }

        private void Contents_MouseDown(object sender, MouseButtonEventArgs e)
        {
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, OuterGlowBitmapEffect.OpacityProperty, 1, TimeSpan.FromMilliseconds(50));
        }

        private void Contents_MouseUp(object sender, MouseButtonEventArgs e)
        {
            BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, OuterGlowBitmapEffect.OpacityProperty, 0, TimeSpan.FromMilliseconds(300));
        }

        private void Contents_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            bool enabled = (bool)e.NewValue;
            if (enabled)
            {
                BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.OpacityProperty, 1, TimeSpan.FromMilliseconds(300));
            }
            else
            {
                BeginAnimationForPresenter<DoubleAnimation, Double>(sender as FrameworkElement, Image.OpacityProperty, 0.3, TimeSpan.FromMilliseconds(300));
            }
        }

        private void bOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ValidateNames = false;
            string gef = FileUriHelper.GetExtensionsFilter(Viewer.GetSupportedExtensions());
            ofd.Filter = string.Format(Properties.Resources.ExtensionsFilter, gef);
            ofd.FilterIndex = lastFilterIndex;
            string[] split = ofd.Filter.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            bool? result = ofd.ShowDialog();
            if (result != null && result.Value)
            {
                lastFilterIndex = ofd.FilterIndex;
                Viewer.CurrentExtensionsFilter = split[(ofd.FilterIndex - 1) * 2 + 1];
                ListOpener.Open(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute));
            }
        }

        private void bPrev_Click(object sender, RoutedEventArgs e)
        {
            ListOpener.OpenPrevious();
        }

        private void bNext_Click(object sender, RoutedEventArgs e)
        {
            ListOpener.OpenNext();
        }

        #endregion

        #region Private helpers

        private void BeginAnimationForPresenter<TAnimation, TValue>(FrameworkElement element, DependencyProperty property, TValue to, TimeSpan duration)
            where TAnimation : AnimationTimeline
        {
            TAnimation anim = Activator.CreateInstance<TAnimation>();
            anim.SetValue(DoubleAnimation.ToProperty, to);
            anim.Duration = new Duration(duration);
            element.BeginAnimation(property, anim);
        }

        #endregion

        #endregion


    }

}